# Copyright (c) 2017, Pawel Bylica
# All rights reserved.

cmake_minimum_required(VERSION 3.0)

include("../../examples/common.cmake")

project(TestStandardFlag)

include(hunter_get_lang_standard_flag)

macro(check LANG EXPECTED)
  set(flag "invalid")
  hunter_get_lang_standard_flag(${LANG} flag)
  string(COMPARE EQUAL "${flag}" "${EXPECTED}" is_correct)
  if(NOT is_correct)
    message(FATAL_ERROR "Expected '${EXPECTED}' standard ${LANG} flag, got '${flag}'")
  endif()
endmacro()

unset(CMAKE_CXX_STANDARD CACHE)
unset(CMAKE_CXX_STANDARD_REQUIRED CACHE)
unset(CMAKE_CXX_EXTENSIONS CACHE)
unset(CMAKE_C_STANDARD CACHE)
unset(CMAKE_C_STANDARD_REQUIRED CACHE)
unset(CMAKE_C_EXTENSIONS CACHE)

check(CXX "")
check(C "")

if(CMAKE_CXX98_EXTENSION_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 98)
  unset(CMAKE_CXX_EXTENSIONS)
  check(CXX "${CMAKE_CXX98_EXTENSION_COMPILE_OPTION}")
  set(CMAKE_CXX_EXTENSIONS On)
  check(CXX "${CMAKE_CXX98_EXTENSION_COMPILE_OPTION}")
endif()

if(CMAKE_CXX98_STANDARD_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 98)
  set(CMAKE_CXX_EXTENSIONS Off)
  check(CXX "${CMAKE_CXX98_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_CXX11_EXTENSION_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 11)
  unset(CMAKE_CXX_EXTENSIONS)
  check(CXX "${CMAKE_CXX11_EXTENSION_COMPILE_OPTION}")
  set(CMAKE_CXX_EXTENSIONS On)
  check(CXX "${CMAKE_CXX11_EXTENSION_COMPILE_OPTION}")
endif()

if(CMAKE_CXX11_STANDARD_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_EXTENSIONS Off)
  check(CXX "${CMAKE_CXX11_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_CXX14_EXTENSION_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 14)
  unset(CMAKE_CXX_EXTENSIONS)
  check(CXX "${CMAKE_CXX14_EXTENSION_COMPILE_OPTION}")
  set(CMAKE_CXX_EXTENSIONS On)
  check(CXX "${CMAKE_CXX14_EXTENSION_COMPILE_OPTION}")
endif()

if(CMAKE_CXX14_STANDARD_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 14)
  set(CMAKE_CXX_EXTENSIONS Off)
  check(CXX "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_CXX17_EXTENSION_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 17)
  unset(CMAKE_CXX_EXTENSIONS)
  check(CXX "${CMAKE_CXX17_EXTENSION_COMPILE_OPTION}")
  set(CMAKE_CXX_EXTENSIONS On)
  check(CXX "${CMAKE_CXX17_EXTENSION_COMPILE_OPTION}")
endif()

if(CMAKE_CXX17_STANDARD_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_EXTENSIONS Off)
  check(CXX "${CMAKE_CXX17_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_C90_EXTENSION_COMPILE_OPTION)
  set(CMAKE_C_STANDARD 90)
  unset(CMAKE_C_EXTENSIONS)
  check(C "${CMAKE_C90_EXTENSION_COMPILE_OPTION}")
  set(CMAKE_C_EXTENSIONS On)
  check(C "${CMAKE_C90_EXTENSION_COMPILE_OPTION}")
endif()

if(CMAKE_C90_STANDARD_COMPILE_OPTION)
  set(CMAKE_C_STANDARD 90)
  set(CMAKE_C_EXTENSIONS Off)
  check(C "${CMAKE_C90_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_C99_EXTENSION_COMPILE_OPTION)
  set(CMAKE_C_STANDARD 99)
  unset(CMAKE_C_EXTENSIONS)
  check(C "${CMAKE_C99_EXTENSION_COMPILE_OPTION}")
  set(CMAKE_C_EXTENSIONS On)
  check(C "${CMAKE_C99_EXTENSION_COMPILE_OPTION}")
endif()

if(CMAKE_C99_STANDARD_COMPILE_OPTION)
  set(CMAKE_C_STANDARD 99)
  set(CMAKE_C_EXTENSIONS Off)
  check(C "${CMAKE_C99_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_C11_EXTENSION_COMPILE_OPTION)
  set(CMAKE_C_STANDARD 11)
  unset(CMAKE_C_EXTENSIONS)
  check(C "${CMAKE_C11_EXTENSION_COMPILE_OPTION}")
  set(CMAKE_C_EXTENSIONS On)
  check(C "${CMAKE_C11_EXTENSION_COMPILE_OPTION}")
endif()

if(CMAKE_C11_STANDARD_COMPILE_OPTION)
  set(CMAKE_C_STANDARD 11)
  set(CMAKE_C_EXTENSIONS Off)
  check(C "${CMAKE_C11_STANDARD_COMPILE_OPTION}")
endif()


# Expect decay to lower standard

if(CMAKE_CXX14_STANDARD_COMPILE_OPTION AND NOT CMAKE_CXX17_STANDARD_COMPILE_OPTION)
  # Decay to c++14
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_EXTENSIONS Off)
  check(CXX "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_CXX11_STANDARD_COMPILE_OPTION AND NOT CMAKE_CXX14_STANDARD_COMPILE_OPTION)
  # Decay to c++11
  set(CMAKE_CXX_EXTENSIONS Off)
  set(CMAKE_CXX_STANDARD 17)
  check(CXX "${CMAKE_CXX11_STANDARD_COMPILE_OPTION}")
  set(CMAKE_CXX_STANDARD 14)
  check(CXX "${CMAKE_CXX11_STANDARD_COMPILE_OPTION}")
endif()

if(CMAKE_C99_STANDARD_COMPILE_OPTION AND NOT CMAKE_C11_STANDARD_COMPILE_OPTION)
  # Decay to c99
  set(CMAKE_C_STANDARD 11)
  set(CMAKE_C_EXTENSIONS Off)
  check(CXX "${CMAKE_C99_STANDARD_COMPILE_OPTION}")
endif()


# Restricted standard
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_C_STANDARD_REQUIRED True)

if(CMAKE_CXX14_STANDARD_COMPILE_OPTION AND NOT CMAKE_CXX17_STANDARD_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_EXTENSIONS Off)
  check(CXX "")
endif()

if(CMAKE_CXX11_STANDARD_COMPILE_OPTION AND NOT CMAKE_CXX14_STANDARD_COMPILE_OPTION)
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_EXTENSIONS Off)
  check(CXX "")
endif()

if(CMAKE_C99_STANDARD_COMPILE_OPTION AND NOT CMAKE_C11_STANDARD_COMPILE_OPTION)
  set(CMAKE_C_STANDARD 11)
  set(CMAKE_C_EXTENSIONS Off)
  check(C "")
endif()
